package com.onlyMe.world.path
{
	import com.onlyMe.geom.Coord;
	
	public class EightWayPathFinder extends FourWayPathFinder
	{
		private var strict:Boolean;

		public function EightWayPathFinder(strict:Boolean=false, limit:int=0)
		{
			super(limit);
			this.strict = strict;
		}
		
		override protected function getAdjacent(coord:Coord):Array
		{
			var coords:Array = super.getAdjacent(coord);
			
			with(coord)
				var diagonals:Array = [
					Coord.create(x + 1, y + 1),
					Coord.create(x + 1, y - 1),
					Coord.create(x - 1, y + 1),
					Coord.create(x - 1, y - 1)
				];
			
			if (!strict)
				return coords.concat(diagonals);
			
			for each (var neighbor:Coord in diagonals)
				if (sidesFree(coord, neighbor))
					coords.push(neighbor);
			
			return coords;
		}
		
		private function sidesFree(coord:Coord, neighbor:Coord):Boolean
		{
			return isFree(Coord.create(coord.x, neighbor.y)) &&
				isFree(Coord.create(neighbor.x, coord.y));
		}
	}
}
